﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AppNet.Models;
using AppNet.MultiLanguage;

namespace AppNet.Areas.Admin.Controllers
{
    public class CompanyController : Controller
    {
        private AppNetDBContext db = new AppNetDBContext();
        private string lang = Translation.GetInstance.Culture; 

        //
        // GET: /Admin/Company/

        public ActionResult Index()
        {
            return View(db.CompanyInfors.Where(c=>c.Lang == lang).First());
        }

        //
        // GET: /Admin/Company/Details/5

        public ActionResult Details(int id = 0)
        {
            CompanyInfor companyinfor = db.CompanyInfors.Find(id);
            if (companyinfor == null)
            {
                return HttpNotFound();
            }
            return View(companyinfor);
        }

        //
        // GET: /Admin/Company/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Admin/Company/Create

        [HttpPost]
        public ActionResult Create(CompanyInfor companyinfor)
        {
            if (ModelState.IsValid)
            {
                companyinfor.Lang = lang;
                string ext = companyinfor.BannerFile.Substring(companyinfor.BannerFile.Length - 3);
                if (ext.Equals("swf"))
                    companyinfor.BannerType = "flash";
                else
                    companyinfor.BannerType = "image";
                db.CompanyInfors.Add(companyinfor);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(companyinfor);
        }

        //
        // GET: /Admin/Company/Edit/5

        public ActionResult Edit(int id = 0)
        {
            CompanyInfor companyinfor = db.CompanyInfors.Find(id);
            if (companyinfor == null)
            {
                return HttpNotFound();
            }
            return View(companyinfor);
        }

        //
        // POST: /Admin/Company/Edit/5

        [HttpPost]
        public ActionResult Edit(CompanyInfor companyinfor)
        {
            if (ModelState.IsValid)
            {
                db.Entry(companyinfor).State = EntityState.Modified;
                companyinfor.Lang = lang;
                string ext = companyinfor.BannerFile.Substring(companyinfor.BannerFile.Length - 3);
                if (ext.Equals("swf"))
                    companyinfor.BannerType = "flash";
                else
                    companyinfor.BannerType = "image";
                db.CompanyInfors.Add(companyinfor);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(companyinfor);
        }

        //
        // GET: /Admin/Company/Delete/5

        public ActionResult Delete(int id = 0)
        {
            CompanyInfor companyinfor = db.CompanyInfors.Find(id);
            if (companyinfor == null)
            {
                return HttpNotFound();
            }
            return View(companyinfor);
        }

        //
        // POST: /Admin/Company/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            CompanyInfor companyinfor = db.CompanyInfors.Find(id);
            db.CompanyInfors.Remove(companyinfor);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}